using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Configuration;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Pavilion;

namespace PavilionGame
{
    public class Engine : Microsoft.Xna.Framework.Game
    {
        private GameController gameController;
        private GraphicsDeviceManager graphics;

        public Engine() : base()
        {
            graphics = new GraphicsDeviceManager(this);
            gameController = new GameController();

            IsMouseVisible = true;
            //this.Activated += (sender, e) => { this.IsMouseVisible = false; };
            //this.Deactivated += (sender, e) => { this.IsMouseVisible = true; };

            GameSession.SessionLoadRequested += (requester, sessionName) =>
            {
                gameController.LoadGameSession(new GameSession(sessionName));

                if (sessionName == "Main")
                    gameController.View = new NarrativeView();
                else
                    gameController.View = new HomeView(this);
            };
        }

        private string LoadResourceDirectory()
        {
            string mainDirectory = AppDomain.CurrentDomain.BaseDirectory;
            mainDirectory = mainDirectory.Substring(0, mainDirectory.Length - 1);

            while (!Directory.Exists(mainDirectory + @"\GameContent\"))
                mainDirectory = Directory.GetParent(mainDirectory).FullName;

            return mainDirectory + @"\GameContent\";
        }

        protected override void Initialize()
        {
            GameEngine.Initialize(this, Services, GraphicsDevice, LoadResourceDirectory());

            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredBackBufferWidth = 1280;

            graphics.ApplyChanges();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            gameController.LoadGameSession(new GameSession("Main"));

            DrawManager.Instance.GameFont = Content.Load<SpriteFont>(@"Content\Fonts\Papyrus");
            gameController.View = new NarrativeView();
            
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            gameController.Update(gameTime);

            if (Keyboard.GetState().IsKeyDown(Keys.H))
                GameSession.RequestSessionLoad(this, "Home");
            else if (Keyboard.GetState().IsKeyDown(Keys.M))
                GameSession.RequestSessionLoad(this, "Main");

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            DrawManager.Instance.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
            gameController.Draw();
            DrawManager.Instance.End();

            base.Draw(gameTime);
        }
    }
}
